package com.supply.dl.widget.span;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.style.ImageSpan;
import android.view.View;

/**
 * 类描述：　[可点击的ImageSpan]<br/>
 * 项目名称：[ForumApp]<br/>
 * 包名：　　[com.ctalk.qmqzzs.widget.span]<br/>
 * 创建人：　[杨挺]<br/>
 * 创建时间：[2015/2/6 16:18]<br/>
 * 公司：　　[成都畅聊科技]<br/>
 * 版本：　　[v1.0.0]<br/>
 */
public abstract class ClickableImageSpan extends ImageSpan
{
	public ClickableImageSpan(Drawable b)
	{
		super(b);
		this.mDrawable = b;
	}

	private Drawable mDrawable;

	public void setDrawable(Drawable mDrawable)
	{
		this.mDrawable = mDrawable;
	}

	@Override
	public Drawable getDrawable()
	{
		return mDrawable;
	}

	@Override
	public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm)
	{
		Drawable d = mDrawable;
		if (d == null)
			return 0;
		Rect rect = d.getBounds();

		if (fm != null)
		{
			fm.ascent = -rect.bottom;
			fm.descent = 0;

			fm.top = fm.ascent;
			fm.bottom = 0;
		}

		return rect.right;
	}

	@Override
	public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint)
	{
		Drawable b = mDrawable;
		if (b == null)
			return;
		canvas.save();

		int transY = bottom - b.getBounds().bottom;

		canvas.translate(x, transY);
		b.draw(canvas);
		canvas.restore();
	}

	public abstract void onClick(View view);
}
